package io.kerns.boot.web.current;

import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 设置当前用户信息拦截器
 *
 * @author xiaohei
 * @create 2020-10-15 上午10:07
 **/
public class CurrentUserInterceptor implements HandlerInterceptor {
    /**
     * 在这里面，把从请求参数或者头里面的信息转换成线程的上下文
     *
     * @param request
     * @param response
     * @param handler
     * @return
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        //TODO 在这里面添加对当前用户数据处理的拦截
        return true;

    }

    /**
     * 请求处理完成之后，删除掉上下文，防止线程复用的脏数据问题
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           @Nullable ModelAndView modelAndView) throws Exception {
        RequestContext.clear();
    }
}
